package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.QueryReportDateRangeRequest;
import com.haohan.jingyuan.customer.dto.resp.CustomerIncomeResp;
import com.haohan.jingyuan.customer.dto.resp.IncomeTypeIncomeResp;
import com.haohan.jingyuan.customer.dto.resp.IndustryDistributionResp;
import com.haohan.jingyuan.customer.dto.resp.MonthlyExpireFileResp;
import com.haohan.jingyuan.customer.dto.resp.TotalCountResp;
import com.haohan.jingyuan.customer.service.CustomerReportService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 客户报表控制器
 *
 * @ClassName CustomerReportController
 * @Description 客户报表相关接口控制器
 * @Author hexijun
 * @Time 2025/01/19 10:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerReportController {

    /**
     * 客户报表服务
     */
    private final CustomerReportService customerReportService;

    /**
     * 查询总数
     *
     * @return 总数统计
     */
    @PostMapping("/customer/report/totalCount")
    @NeedLogin
    public GwResponse<TotalCountResp> queryTotalCount() {
        log.info("CustomerReportController.queryTotalCount BEGIN");

        TotalCountResp totalCount = customerReportService.queryTotalCount();
        GwResponse<TotalCountResp> response = GwResponse.success("查询总数成功", totalCount);

        log.info("CustomerReportController.queryTotalCount END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 服务类型收入
     *
     * @param request 查询日期范围请求
     * @return 服务类型收入列表
     */
    @PostMapping("/customer/report/incomeTypeIncome")
    @NeedLogin
    public GwResponse<List<IncomeTypeIncomeResp>> queryIncomeTypeIncome(@Valid @RequestBody GwRequest<QueryReportDateRangeRequest> request) {
        log.info("CustomerReportController.queryIncomeTypeIncome BEGIN ==> {}", JSONUtil.toJsonStr(request));

        List<IncomeTypeIncomeResp> incomeTypeIncomeList = customerReportService.queryIncomeTypeIncome(request.getRequestData());
        GwResponse<List<IncomeTypeIncomeResp>> response = GwResponse.success("查询服务类型收入成功", incomeTypeIncomeList);

        log.info("CustomerReportController.queryIncomeTypeIncome END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 客户收入
     *
     * @param request 查询日期范围请求
     * @return 客户收入列表
     */
    @PostMapping("/customer/report/customerIncome")
    @NeedLogin
    public GwResponse<List<CustomerIncomeResp>> queryCustomerIncome(@Valid @RequestBody GwRequest<QueryReportDateRangeRequest> request) {
        log.info("CustomerReportController.queryCustomerIncome BEGIN ==> {}", JSONUtil.toJsonStr(request));

        List<CustomerIncomeResp> customerIncomeList = customerReportService.queryCustomerIncome(request.getRequestData());
        GwResponse<List<CustomerIncomeResp>> response = GwResponse.success("查询客户收入成功", customerIncomeList);

        log.info("CustomerReportController.queryCustomerIncome END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 客户行业分布
     *
     * @return 客户行业分布列表
     */
    @PostMapping("/customer/report/industryDistribution")
    @NeedLogin
    public GwResponse<List<IndustryDistributionResp>> queryIndustryDistribution() {
        log.info("CustomerReportController.queryIndustryDistribution BEGIN");

        List<IndustryDistributionResp> industryDistributionList = customerReportService.queryIndustryDistribution();
        GwResponse<List<IndustryDistributionResp>> response = GwResponse.success("查询客户行业分布成功", industryDistributionList);

        log.info("CustomerReportController.queryIndustryDistribution END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 材料到期时间分布
     *
     * @return 月度到期文件列表
     */
    @PostMapping("/customer/report/monthlyExpireFile")
    @NeedLogin
    public GwResponse<List<MonthlyExpireFileResp>> queryMonthlyExpireFile() {
        log.info("CustomerReportController.queryMonthlyExpireFile BEGIN");

        List<MonthlyExpireFileResp> monthlyExpireFileList = customerReportService.queryMonthlyExpireFile();
        GwResponse<List<MonthlyExpireFileResp>> response = GwResponse.success("查询材料到期时间分布成功", monthlyExpireFileList);

        log.info("CustomerReportController.queryMonthlyExpireFile END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}

